<?php
class GoodAction extends MbaseAction {
	public $tbname = 'Good';
	public $obj = '';
	public $mpk = 'good_id';
	public $cate = 'good_cate';

	public function getMap() {
		$maparr = $_GET;
		$p = isset ($_GET['p']) ? $_GET['p'] : 1;
		$ps = isset ($_GET['ps']) ? $_GET['ps'] : 15;
		$oby = isset ($_GET['oby']) ? $_GET['oby'] : '';
		$okey = isset ($_GET['okey']) ? $_GET['okey'] : 'desc';

		$ps = 16;
		$cate = isset ($_GET['cate']) ? $_GET['cate'] : 0;
		$page['p'] = $p;

		$map = array (
			'p' => $p,
			'ps' => $ps
		);
		if (!empty ($oby)) {
			$map['o'] = array (
				'oby' => $oby,
				'okey' => $okey
			);
			$page['oby'] = $oby;
			$page['okey'] = $okey;
		}
		$map['q'] = array ();
		$this->assign('nowcate', null);
		$par = $map;
		if (!empty ($cate)) {
			$page['cate'] = $cate;
			$par['q']['cate'] = $cate;
			$gcates = $this->getCateTree();
			$cates = $gcates['i'][$cate];
			$this->assign('icate', $gcates[$cate]);
			$i = 1;
			foreach ($cates['parr'] as $v) {
				if ($v == 0)
					break;
				$map['q']['cate_' . $i] = $v;
				$i++;
			}
		}
		$price = isset ($_GET['fee']) ? trim($_GET['fee']) : 0;
		if ($price) {
			$par['q']['fee'] = $price;
			$pall = 0;
			switch ($price) {
				case 'lt99' :
					$map['q']['shop_fee'] = array (
						'lt',
						100
					);
					break;
				case '100to199' :
					$map['q']['shop_fee'] = array (
						array (
							'egt',
							100
						),
						array (
							'lt',
							200
						)
					);
					;
					break;
				case '200to299' :
					$map['q']['shop_fee'] = array (
						array (
							'egt',
							200
						),
						array (
							'lt',
							300
						)
					);
					;
					break;
				case '300to499' :
					$map['q']['shop_fee'] = array (
						array (
							'egt',
							300
						),
						array (
							'lt',
							500
						)
					);
					;
					break;
				case 'gt500' :
					$map['q']['shop_fee'] = array (
						'gt',
						500
					);
					break;
				default :
					break;
			}
		}
		$this->map = $map;
		$this->assign('urlmap', $map);
		$this->assign('urlpar', $par);
		return array (
			'par' => $par,
			'map' => $map,
			'page' => $page
		);
	}

	public function index() {
		$this->getMap();
		$this->getHotlist();
		$cates = $this->getCateTree('good_cate');
		$nowcate = isset ($_GET['cate']) ? trim($_GET['cate']) : 0;
		$all = 0;
		$subcates = array ();
		$bigcate = null;
		if ($nowcate) {
			$cateinfo = $cates['i'][$nowcate];
			if ($cateinfo['parent_id'] == 0) {
				$all = 1;
				foreach ($cates['t'] as $vo) {
					if ($vo['parent_id'] == $cateinfo['cate_id'])
						$subcates[] = $vo;
				}
				$bigcate = $cateinfo;
			} else {
				$all = 0;
				$bigcate = $cates['t'][$cateinfo['parent_id']];
				foreach ($cates['t'] as $vo) {
					$vo['alive'] = 0;
					if ($vo['enname'] == $nowcate)
						$vo['alive'] = 1;
					if ($vo['parent_id'] == $bigcate['cate_id'])
						$subcates[] = $vo;
				}
			}
			$this->setSeo($cateinfo['title'],$cateinfo['keyword'],$cateinfo['detail']);
		} else {
			$all = 1;
			foreach ($cates['t'] as $vo) {
				if ($vo['parent_id'] == 0)
					$subcates[] = $vo;
			}
			$this->setSeo('搜索','','');
		}
		$this->assign('all', $all);
		$this->assign('bigcate', $bigcate);
		$this->assign('subcates', $subcates);
		$pall = isset ($_GET['fee']) ? 0 : 1;
		$this->assign('pall', $pall);
		$maparr = $this->getMap();
		$datalist = $this->obj->doGet($maparr['map']);
		$this->assign('datalist', $datalist['list']);
		$this->assign('pageinfo', $datalist['page']);
		$this->initPage($datalist['page'], $maparr['page']);
		$this->display();
	}
	public function show() {
		cookie('jumpurl', __SELF__);
		$good_id = isset ($_GET['good_id']) ? trim($_GET['good_id']) : 0;
		if ($good_id) {
			$mcomment = M('Comment');
			$map['good_id'] = $good_id;
			$map['progress'] = 1;
			$commentnum = $mcomment->where($map)->count();
			unset ($map['progress']);
			$this->assign('comment_num', $commentnum);
			$morder = M('Good_order');
			$recordnum = $morder->where($map)->count('good_id');
			$this->assign('record_num', $recordnum);
		}
		$this->assign('isgood', 1);
		$mdeal = M('Good_order');
		$map['good_id'] = $good_id;
		$map['dealtime'] = array (
			'gt',
			time() - 15811200
		);

		$ocount = $mdeal->where($map)->sum('good_num');
		$this->assign('ordercount', $ocount);
		$gmap = array(
			'p' => 0,
			'q' => array(
				'good_id' => $good_id
			)
		);
		
		$datainfo = $this->obj->doGet($gmap);
		$cates = $this->getCateTree('good_cate');
		$bigcate = $cates['t'][$datainfo['cate_1']];
		$subcate = empty($datainfo['cate_2'])? null:$cates['t'][$datainfo['cate_2']];
		$this->assign('bigcate', $bigcate);
		$this->assign('subcate', $subcate);
		$this->assign('datainfo',$datainfo);
		$this->display();
	}
	public function comment() {
		$good_id = isset ($_GET['good_id']) ? trim($_GET['good_id']) : 0;
		$p = isset ($_GET['p']) ? trim($_GET['p']) : 1;
		$ps = 10;
		$mcomment = D('Comment');
		$map['good_id'] = $good_id;
		$map['progress'] = 1;
		$count = $mcomment->where($map)->count();
		$pageinfo = initPage($count, $p, $ps);
		$datalist = $mcomment->relation('member')->where($map)->order('comment_id desc')->limit($ps)->page($p)->select();
		$this->assign('commentlist', $datalist);
		$this->assign('page', $pageinfo);
		$this->display();
	}

	public function record() {
		//使用Deal
		$good_id = isset ($_GET['good_id']) ? trim($_GET['good_id']) : 0;
		$p = isset ($_GET['p']) ? trim($_GET['p']) : 1;
		$ps = 10;
		$mdeal = M('Good_order');
		$map['good_id'] = $good_id;
		$map['dealtime'] = array (
			'gt',
			time() - 15811200
		);
		$count = $mdeal->where($map)->count();
		$pageinfo = initPage($count, $p, $ps);
		$datalist = $mdeal->where($map)->order('deal_id desc')->limit($ps)->page($p)->select();
		$this->assign('deallist', $datalist);
		$this->assign('page', $pageinfo);
		$this->display();
	}
	public function relas() {
		$good_id = isset ($_GET['good_id']) ? trim($_GET['good_id']) : 0;
		$path = isset ($_GET['cate_id']) ? trim($_GET['cate_id']) : '';
		$qmap['p'] = 1;
		$qmap['ps'] = 9;
		$qmap['good_id'] = array (
			'neq',
			$good_id
		);
		$qmap['cate_id'] = $path;
		$datalist = $this->obj->doGet($qmap);
		$imap['good_id'] = $good_id;
		$ioid = $this->obj->where($imap)->getField('oid');
		$idataarr1 = array ();
		$idataarr2 = array ();
		if ($ioid) { //5   3456789
			foreach ($datalist['list'] as $adata) {
				if ($adata['oid'] >= $ioid) {
					$idataarr1[] = $adata;
				} else {
					$idataarr2[] = $adata;
				}
			}
			$idataarr = array_merge($idataarr1, array_reverse($idataarr2));
		} else {
			$idataarr = $datalist;
		}

		$this->assign('datalist', $idataarr);
		$this->display();
	}

	public function getHotlist() {
		$mgood = D('Good');
		$map = array (
			'q' => $this->map['q'],
			'o' => array (
				'oby' => 'order_count',
				'okey' => 'desc'
			),
			'p' => 1,
			'ps' => 10
		);
		$sellgoods = $mgood->doGet($map);
		$this->assign('selllist', $sellgoods['list']);
	}

	public function getCateTree($catename = '', $reset = 0) {
		if ($this->cate == '' && $catename == '')
			return null;
		if (!empty ($catename))
			$this->cate = $catename;
		if (empty ($this->cate))
			return null;
		$cate = $this->cate . 's';
		$mcatetb = ucfirst(strtolower($this->cate));
		$tcateher = array ();
		if (S($cate) && $reset == 0) {
			$tcateher = S($cate);
		} else {
			$mcate = D($mcatetb);
			$tcateher = $mcate->doGet();
			S($cate, $tcateher);
		}
		$this->assign($cate, $tcateher);
		$tarr = array ();
		foreach ($tcateher as $vo) {
			$tarr[$vo['enname']] = $vo;
		}
		$this->assign($cate . 'i', $tarr);
		return array (
			't' => $tcateher,
			'i' => $tarr
		);
	}
}
?>